package com.quectel.cms.controller.patrol;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.patrol.dto.VideoPatrolPlanDto;
import com.quectel.core.module.patrol.dto.VideoPatrolRouteDto;
import com.quectel.core.module.patrol.service.VideoPatrolPlanService;
import com.quectel.core.module.patrol.service.VideoPatrolRouteService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2020-07-07 15:47:28
 */
@RestController
@RequestMapping("videoPatrolPlan")
@Tag(name = "VideoPatrolPlanController", description = "视频巡更计划相关api")
public class VideoPatrolPlanController extends BaseController {

    @DubboReference
    private VideoPatrolPlanService videoPatrolPlanService;
    @DubboReference
    private VideoPatrolRouteService videoPatrolRouteService;


    /**
     * 视频巡更分页查询列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "videoPatrolPlan:list")
    @Operation(summary = "视频巡更分页查询列表")
    public Response<Page<VideoPatrolPlanDto>> list(@RequestParam Map<String, Object> params) {

        super.fillAuthorityData(params);
        //查询列表数据
        Request request = Request.configParams(params).like("name");

        int total = videoPatrolPlanService.queryTotal(params);

        List<VideoPatrolPlanDto> list = null;
        if (total > 0) {
            list = videoPatrolPlanService.queryList(params);
        } else {
            list = new ArrayList<>();
        }

        Page<VideoPatrolPlanDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<VideoPatrolPlanDto>>ok().wrap(page);
    }


    /**
     * 根据id查询视频巡更信息
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "videoPatrolPlan:info")
    @Operation(summary = "根据id查询视频巡更信息")
    public Response<VideoPatrolPlanDto> info(@PathVariable("id") Long id) {

        VideoPatrolPlanDto videoPatrolPlanDto = videoPatrolPlanService.selectById(id);

        return Response.<VideoPatrolPlanDto>ok().wrap(videoPatrolPlanDto);
    }

    /**
     * 保存视频巡更信息
     */
    @PostMapping("save")
    @RequirePermissions(values = "videoPatrolPlan:edit")
    @Operation(summary = "保存视频巡更信息")
    public Response<Object> save(@RequestBody @Validated VideoPatrolPlanDto videoPatrolPlanDto) {

        //填充基础数据
        fillSaveCommonData(videoPatrolPlanDto);

        VideoPatrolRouteDto videoPatrolRouteDto = videoPatrolRouteService.selectById(Long.valueOf(videoPatrolPlanDto.getVideoPatrolRouteId()));
        if (videoPatrolRouteDto != null) {
            //填充小区
            videoPatrolPlanDto.setVillageId(videoPatrolRouteDto.getVillageId());
        }

        videoPatrolPlanService.save(videoPatrolPlanDto);

        return Response.ok();
    }

    /**
     * 修改视频巡更信息
     */
    @PostMapping("update")
    @RequirePermissions(values = "videoPatrolPlan:edit")
    @Operation(summary = "修改视频巡更信息")
    public Response<Object> update(@RequestBody @Validated VideoPatrolPlanDto videoPatrolPlanDto) {

        //填充基础数据
        fillEditCommonData(videoPatrolPlanDto);

        VideoPatrolRouteDto videoPatrolRouteDto = videoPatrolRouteService.selectById(Long.valueOf(videoPatrolPlanDto.getVideoPatrolRouteId()));
        if (videoPatrolRouteDto != null) {
            //填充小区
            videoPatrolPlanDto.setVillageId(videoPatrolRouteDto.getVillageId());
        }

        videoPatrolPlanService.updateById(videoPatrolPlanDto);

        return Response.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequirePermissions(values = "videoPatrolPlan:delete")
    @Operation(summary = "批量删除视频巡更信息")
    public Response<Object> delete(@RequestBody Long[] ids) {

        videoPatrolPlanService.deleteBatch(ids);

        return Response.ok();
    }
}
